package lainey.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.Calendar;

import lainey.ui.R;

public class DateDialog extends Dialog {

    private TextView mTvTitle;
    private TextView mTvLeft;
    private TextView mTvRight;
    private DatePicker mDatePicker;

    public DateDialog(@NonNull Context context) {
        super(context, R.style.UI_DefaultDialogStyle);
        initDialog(context);
    }

    private void initDialog(Context context) {
        setContentView(R.layout.ui_dialog_date);
        Window window = getWindow();
        if (window != null) {
            window.getAttributes().gravity = Gravity.CENTER;
        }
        setCancelable(false);
        setCanceledOnTouchOutside(true);
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            setOwnerActivity(activity);
        }

        mTvTitle = findViewById(R.id.tv_title);
        mTvLeft = findViewById(R.id.tv_left);
        mTvRight = findViewById(R.id.tv_right);
        mDatePicker = findViewById(R.id.date_picker);

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        mDatePicker.init(year, month, day, null);
    }

    public void setLeftOnClickListener(View.OnClickListener listener) {
        mTvLeft.setOnClickListener(listener);
    }

    public void setRightOnClickListener(View.OnClickListener listener) {
        mTvRight.setOnClickListener(listener);
    }

    public void setInitDate(int year, int month, int day) {
        mDatePicker.init(year, month, day, null);
    }

    public DatePicker getDatePicker() {
        return mDatePicker;
    }

    public void setTitle(String text) {
        mTvTitle.setText(text);
    }

    public void setTitleText(int resId) {
        mTvTitle.setText(resId);
    }

    public void setLeftText(String text) {
        mTvLeft.setText(text);
    }

    public void setLeftText(int redId) {
        mTvLeft.setText(redId);
    }

    public void setRightText(String text) {
        mTvRight.setText(text);
    }

    public void setRightText(int resId) {
        mTvRight.setText(resId);
    }

    /**
     * 设置时间选择器的分割线颜色
     */
    public void setDatePickerDividerColor(int color) {
        try {
            // 获取 mSpinners
            LinearLayout llFirst = (LinearLayout) mDatePicker.getChildAt(0);
            // 获取 NumberPicker
            LinearLayout spinners = (LinearLayout) llFirst.getChildAt(0);
            for (int i = 0; i < spinners.getChildCount(); i++) {
                NumberPicker picker = (NumberPicker) spinners.getChildAt(i);
                Field[] pickerFields = NumberPicker.class.getDeclaredFields();
                for (Field field : pickerFields) {
                    field.setAccessible(true);
                    boolean equals = field.getName().equals("mSelectionDivider");
                    if (equals) {
                        ColorDrawable drawable = new ColorDrawable(color);
                        field.set(picker, drawable);
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
